package com.ny.Map;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        List<Aot> aotList = new ArrayList<>();
        initializeEmployees(aotList);

        Scanner scanner = new Scanner(System.in);
        boolean running = true;
        Testlist testlist = new Testlist();

        while (running) {
            displayMenu();
            System.out.print("请选择你需要进行的操作: ");

            try {
                int choice = scanner.nextInt();
                scanner.nextLine(); // Consume newline

                switch (choice) {
                    case 0:
                        running = false;
                        System.out.println("系统退出，感谢使用！");
                        break;
                    case 1:
                        addEmployee(aotList, scanner, testlist);
                        break;
                    case 2:
                        testlist.findAllEmp(aotList);
                        break;
                    case 3:
                        deleteEmployee(aotList, scanner, testlist);
                        break;
                    case 4:
                        updateEmployee(aotList, scanner, testlist);
                        break;
                    default:
                        System.out.println("无效的选择，请重新输入！");
                }
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入！");
                scanner.nextLine(); // Clear invalid input
            }
        }
        scanner.close();
    }

    private static void initializeEmployees(List<Aot> list) {
        list.add(new Aot("250", "艹日大大可", 250, ""));
        list.add(new Aot("150", "苏火山", 150, ""));
        list.add(new Aot("138", "奥特曼", 250, ""));
        list.add(new Aot("10086", "孙绍木水", 250, ""));
    }

    private static void displayMenu() {
        System.out.println("\n欢迎来到员工信息管理系统");
        System.out.println("0.退出系统");
        System.out.println("1.添加员工信息");
        System.out.println("2.查询员工信息");
        System.out.println("3.删除员工信息");
        System.out.println("4.修改员工信息");
    }

    private static void addEmployee(List<Aot> list, Scanner scanner, Testlist testlist) {
        System.out.println("请输入员工信息");
        System.out.print("请输入员工编号: ");
        String number = scanner.nextLine();

        System.out.print("请输入员工姓名: ");
        String name = scanner.nextLine();

        System.out.print("请输入员工年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        testlist.addEmp(list, new Aot(number, name, age, ""));
        System.out.println("员工添加成功！");
    }

    private static void deleteEmployee(List<Aot> list, Scanner scanner, Testlist testlist) {
        System.out.print("请输入需要删除的员工编号: ");
        String num = scanner.nextLine();

        if (testlist.deleteEmpByNumber(list, num)) {
            System.out.println("员工删除成功！");
        } else {
            System.out.println("未找到该员工编号！");
        }
    }

    private static void updateEmployee(List<Aot> list, Scanner scanner, Testlist testlist) {
        System.out.print("请输入需要修改的员工编号: ");
        String number = scanner.nextLine();

        Aot aot = testlist.findEmpByNumber(list, number);
        if (aot == null) {
            System.out.println("未找到该员工！");
            return;
        }

        System.out.print("请输入新的年龄: ");
        int newAge = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        aot.setAge(newAge);
        testlist.updateEmpAge(list, aot);
        System.out.println("员工信息修改成功！");
    }
}
